﻿

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using SWDataDB.Models;
using SWDataDB.BLL;

using SWDataDB.DAL;

public partial class AdminManage_Manage_UserManage : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Check_Login();
            Bind();
        }
    }
    /// <summary>
    /// 绑定数据到 GridView 
    /// </summary>
    void Bind()
    {
        DataSet ds = GetData();
         
        gvData.DataSource = ds;
        gvData.DataKeyNames = new string[] { "USER_ID" };
        gvData.DataBind();
        
    }
    /// <summary>
    /// 处理模式更改的事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void dvData_ModeChanging(object sender, DetailsViewModeEventArgs e)
    {
        if (e.CancelingEdit)
        {
            if (e.NewMode == DetailsViewMode.Insert)
            {
                Bind();
            }
        }
    }
    /// <summary>
    /// insert
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void dvData_ItemInserting(object sender, DetailsViewInsertEventArgs e)
    {
        // 页面逻辑验证通过后获取数据
        DetailsView dv = sender as DetailsView;

        UserTbl zktbl = new UserTbl();
        USER_TBL userInf=new USER_TBL(); 
        userInf.O_LOGIN_NAME = (dv.FindControl("LOGIN_NAME") as TextBox).Text;
        userInf.O_PASSWORD = (dv.FindControl("PASSWORD") as TextBox).Text;
        userInf.O_REAL_NAME = (dv.FindControl("REAL_NAME") as TextBox).Text;
        userInf.O_GROUP = int.Parse(Request.Form["USERGROUP"]);

        zktbl.AddUser(userInf);

        Bind();

        //if (null != txtName && null != txtDes && null != ddlCatalog)
        //{
        //     name = txtName.Text.Trim();
        //     catalogID = ddlCatalog.SelectedValue;
        //     description = txtDes.Text.Trim();
            
        //}
        // 逻辑验证...
        // ...
        // 执行插入...
        //DataSet ds = GetData();
        //DataTable dt = ds.Tables[0];
        //DataRow dr = dt.NewRow();
        //dr[0] = Guid.NewGuid();
        //dr[1] = name;//computer
        //dr[2] = 2;
        //dr[3] = description;
        //dt.Rows.Add(dr);
        
       
    }
    #region// 模拟数据
    ///// <summary>
    ///// 产品列表(模拟数据)
    ///// </summary>
    ///// <returns></returns>
    //DataTable ProductTable()
    //{
    //    DataTable tb = new DataTable();
    //    DataColumn col = new DataColumn("ProductID", typeof(Guid)) { Unique = true, AllowDBNull = false };
    //    tb.Columns.Add(col);
    //    tb.Columns.Add(new DataColumn("Name", typeof(string)));
    //    tb.Columns.Add(new DataColumn("CatalogID", typeof(int)));
    //    tb.Columns.Add(new DataColumn("Description", typeof(string)));
    //    tb.PrimaryKey = new DataColumn[] { col };
    //    DataRow dr = tb.NewRow();
    //    dr[0] = Guid.NewGuid();
    //    dr[1] = "G450";//computer
    //    dr[2] = 1;
    //    dr[3] = "lenovo note pad";
    //    tb.Rows.Add(dr);
    //    dr = tb.NewRow();
    //    dr[0] = Guid.NewGuid();
    //    dr[1] = "TV";//electrical furniture
    //    dr[2] = 2;
    //    dr[3] = "SAMSUNG";
    //    tb.Rows.Add(dr);
    //    dr = tb.NewRow();
    //    dr[0] = Guid.NewGuid();
    //    dr[1] = "desk";//others
    //    dr[2] = 3;
    //    dr[3] = "none";
    //    tb.Rows.Add(dr);
    //    return tb;
    //}
    // <summary>
    // 产品类别(模拟数据)
    // </summary>
    // <returns></returns>
    //protected DataTable CatalogTable()
    //{
    //    DataTable tb = new DataTable();
    //    DataColumn col1 = new DataColumn("CatalogID", typeof(int)) { AllowDBNull = false, AutoIncrement = true, AutoIncrementSeed = 1, AutoIncrementStep = 1 };
    //    DataColumn col2 = new DataColumn("Name", typeof(string)) { Unique = true, AllowDBNull = false };
    //    tb.Columns.Add(col1);
    //    tb.Columns.Add(col2);
    //    tb.PrimaryKey = new DataColumn[] { col1 };
    //    DataRow dr = tb.NewRow();
    //    dr["Name"] = "computer";
    //    tb.Rows.Add(dr);
    //    dr = tb.NewRow();
    //    dr["Name"] = "electrical furniture";
    //    tb.Rows.Add(dr);
    //    dr = tb.NewRow();
    //    dr["NAME"] = "others";
    //    tb.Rows.Add(dr);
    //    return tb;
    //}
    // <summary>
    // 获取要绑定的数据
    // </summary>
    // <returns></returns>
    DataSet GetData()
    {
         
        DataSet ds = new DataSet();
        SWDataDB.DAL.UserTbl userobj = new UserTbl();
        ds = userobj.GetAllUserDS();
        return ds;
    }
    #endregion

    /// <summary>
    /// 处理用户单击"添加用户"按钮的事件,跳转到 EmptyDataTemplate 模板
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void OnAddRecordClick(object sender, EventArgs e)
    {
        gvData.DataSource = null;



        gvData.DataBind();
    }

    protected void gvData_RowEditing(object sender, GridViewEditEventArgs e)
    {

        gvData.EditIndex = e.NewEditIndex;
        Bind();
    }





    protected void gvData_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
       
        
        string strPsw = ((TextBox)(gvData.Rows[e.RowIndex].Cells[1].Controls[0])).Text.ToString().Trim();
        string UName = ((TextBox)(gvData.Rows[e.RowIndex].Cells[2].Controls[0])).Text.ToString().Trim();
        string RealName = ((TextBox)(gvData.Rows[e.RowIndex].Cells[3].Controls[0])).Text.ToString().Trim();
        string Group = ((TextBox)(gvData.Rows[e.RowIndex].Cells[4].Controls[0])).Text.ToString().Trim();

        int UID = Convert.ToInt32(gvData.DataKeys[e.RowIndex].Value);
        UserTbl userUP = new UserTbl();
        userUP.UpdateUser(UID, strPsw, UName, RealName, Convert.ToInt32(Group));
        //int UID,string strPsw,string UName,string RName,int UGROUP
         
        gvData.EditIndex = -1;
        Bind();
    }


    protected void gvData_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        gvData.EditIndex = -1;
        Bind();
    }

    /// <summary>
    /// 在单击 GridView 控件内某一行的 Delete 按钮（其 CommandName 属性设置为"Delete"的按钮）时发生，但在 GridView 控件从数据源删除记录之前。此事件通常用于取消删除操作。
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void gvData_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        UserTbl UerDel = new UserTbl();
        int delID = Convert.ToInt32(gvData.DataKeys[e.RowIndex].Value);
        UerDel.DelUser(delID);
        Bind();
    }


    public void Check_Login()
    {
        //判断存储在Session对象中


        if ((Session["UserName"] == null))
        {
            Response.Write("<script>alert('对不起！请先登录！'); window.top.location.href='../LoginIndex.aspx'</script>");

            Response.End();
        }
        else
        {
            string strName = Session["UserName"].ToString();
            UserTbl strUerName = new UserTbl();

            USER_TBL userinfo=new USER_TBL();
            userinfo=strUerName.SelectUser(strName);
            if (userinfo.O_GROUP!= 1)
            {
                TableContent.Attributes["style"] = "display:none";
                ComUserDIV.Attributes["style"] = "display:'';text-align:center;font-family:'微软雅黑';font-size:16px;color:red;width:100%;height:100%;padding:100px 0px 0px 0px;";
            }
        }
    }

}
